package com.ruicar.afs.cloud.product.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.product.condition.ProductPlanCondition;
import com.ruicar.afs.cloud.product.entity.ProductUpdateAtom;
import com.ruicar.afs.cloud.product.enums.ATOM_CONTROL_TYPE;
import com.ruicar.afs.cloud.product.mapper.ProductUpdateAtomMapper;
import com.ruicar.afs.cloud.product.service.ProductUpdateAtomService;
import com.ruicar.afs.cloud.product.vo.ProductPlanVo;
import com.ruicar.afs.cloud.product.vo.VueJs;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xudongdong
 * @version 1.0
 * @date 2020-05-26
 */
@Service
public class ProductUpdateAtomServiceImpl extends ServiceImpl<ProductUpdateAtomMapper, ProductUpdateAtom> implements ProductUpdateAtomService {
    /**
     * 保存产品方案数据修改表
     *
     * @param object
     * @param id
     */
    @Override
    public void saveData(Object object, Long id, List<VueJs> list) {
        LinkedHashMap<String, Object> set = (LinkedHashMap) object;
        for (Map.Entry<String, Object> entry : set.entrySet()) {
            String mapKey = String.valueOf(entry.getKey());
            String mapValue = String.valueOf(entry.getValue());
        }
    }

    public Map getProductPlanDataInfo(ProductPlanCondition productPlanId) {
        List<ProductPlanVo> list = this.baseMapper.getProductPlanData(productPlanId);
        Map<String, Object> map = new HashMap<>();
        list.stream().forEach(
                productPlanVo -> {
                    Object value = null;
                    if (StrUtil.isNotBlank(productPlanVo.getValue())) {
                        String type = ATOM_CONTROL_TYPE.getType(productPlanVo.getType());
                        if (ATOM_CONTROL_TYPE.DECIMAL.getType().equals(type)) {
                            value = new BigDecimal(productPlanVo.getValue());
                        } else if (ATOM_CONTROL_TYPE.SCALE.getType().equals(type)) {
                            value = new BigDecimal(productPlanVo.getValue());
                        } else if (ATOM_CONTROL_TYPE.AMOUNT.getType().equals(type)) {
                            value = new BigDecimal(productPlanVo.getValue());
                        } else if (ATOM_CONTROL_TYPE.DATE.getType().equals(type)) {
                            value = DateUtil.parse(productPlanVo.getValue(), "yyyy-MM-dd");
                        } else if (ATOM_CONTROL_TYPE.INTEGER.getType().equals(type)) {
                            value = Integer.valueOf(productPlanVo.getValue());
                        } else if (ATOM_CONTROL_TYPE.TIME.getType().equals(type)) {
                            value = DateUtil.parse(productPlanVo.getValue(), "HH:mm:ss");
                        } else if (ATOM_CONTROL_TYPE.CHECK_BOX.getType().equals(type) || ATOM_CONTROL_TYPE.MULTIPLE_SELECT.getType().equals(type)) {
                            String atomValue = productPlanVo.getValue().toString();
                            atomValue = atomValue.replace("[", "");
                            atomValue = atomValue.replace("]", "");
                            atomValue = atomValue.replace(" ", "");
                            value = atomValue.split(",");
                        } else {
                            value = productPlanVo.getValue();
                        }
                    }
                    map.put(productPlanVo.getKey(), value);
                }
        );
        return map;
    }

    /**
     * 保存或更新产品方案数据表
     * @param object
     * @param id
     */
    public void saveOrUpdate(Object object, Long id) {
        LinkedHashMap<String, Object> set = (LinkedHashMap) object;
        for (Map.Entry<String, Object> entry : set.entrySet()) {
            String mapKey = entry.getKey();
            if (StrUtil.isNotBlank(String.valueOf(entry.getValue())) && entry.getValue() != null) {
                String mapValue = String.valueOf(entry.getValue());
                ProductUpdateAtom data = new ProductUpdateAtom(id, mapKey, mapValue);
                this.save(data);
            }
        }
    }

}
